python获取类名函数名、脚本路径 您所在的位置:网站首页 python 获取调用函数名 python获取类名函数名、脚本路径

python获取类名函数名、脚本路径

2023-07-01 06:20| 来源: 网络整理| 查看: 265

1).python获取当前运行的类名函数名

import inspect def get_current_function_name():     return inspect.stack()[1][3] class MyClass:     def function_one(self):         print "%s.%s invoked"%(self.__class__.__name__, get_current_function_name()) if __name__ == "__main__":     myclass = MyClass()     myclass.function_one()

动态获取当前运行的函数名很方便,特别是对于一些debug系统来说

2).获取脚本名(三种方式)

import os,sys,inspect def path():         return inspect.getfile(sys.modules[__name__]) def path2():         return inspect.getfile(inspect.currentframe()) def path3():         return inspect.stack()[1][1]          print path() print path2() print path3()

打印结果:

34.py

34.py

34.py

分——割——————线——————————————————————

以下三个py文件是有联系的,后面一个引用前面一个中的相关函数,测试顺序pathtest.py-->aaa.py-->test01.py,通过它们的打印结果,可以印证python获取路径的三种情况。

|---bin     |---test01.py   |---src     |---aaa.py   |---testcase     |---group01        |---pathtest.py

Python获取路径的三种情况:获取函数定义所在脚本路径,获取调用函数脚本所在路径,获取启动Python解释器脚本所在路径。最常用的方式就是pathtest.py中current_path中获取路径的方式。inspect.getfile(inspect.currentframe())和inspect.stack()[0][1]得到的是定义该函数的脚本文件的路径,包含脚本名称 inspect.stack()[1][1]得到的是调用该脚本的脚本文件的路径,包含脚本名称os.path.dirname(os.path.realpath(sys.argv[0]))得到的是执行python x.py命令时,脚本参数x.py(也就是argv[0])所在路径os.path.dirname(os.path.realpath(sys.path[0])),根据sys.path[0],sys.path[1],sys.path[2]取不同的值,获取调用函数脚本所在路径或者获取启动Python解释器脚本所在路径

1.  pathtest.py

获取脚本路径(五种方式):

cd /soft/testcase/group01

vim pathtest.py

#coding:utf-8 import os,sys,inspect def path():         dir = inspect.getfile(sys.modules[__name__])         return os.path.abspath(os.path.dirname(dir)) def path1():         dir = os.path.realpath(sys.argv[0])         if os.path.isfile(dir):                 path=os.path.dirname(dir)         return os.path.abspath(path) def path2():         this_file = inspect.getfile(inspect.currentframe())#得到定义该函数的脚本路径         return os.path.abspath(os.path.dirname(this_file)) def script_path():         caller_file = inspect.stack()[0][1]#stack()[0][1]得到定义该函数的脚本路径,stack()[1][1]获取调用该函数的脚本所在路.径         return os.path.abspath(os.path.dirname(caller_file)) def current_path():         dir = os.path.realpath(sys.path[0])         if os.path.isfile(dir):                 path=os.path.dirname(dir)                 return os.path.abspath(path)         else:                 caller_file=inspect.stack()[1][1]                 return os.path.abspath(os.path.dirname(caller_file)) #print path() #print path1() #print path2() #print script_path() #print current_path()

打印结果:

/soft/testcase/group01

/soft/testcase/group01

/soft/testcase/group01

/soft/testcase/group01

/soft/testcase/group01

2.  aaa.py

cd /soft/src

vim aaa.py

import os import sys import inspect sys.path.append("../testcase/group01") import pathtest def caller_func():         caller_path=pathtest.script_path()         return caller_path def def_func():         def_path=pathtest.path2()         return def_path def real_func():         real_func=pathtest.path1()         return real_func def curr_func():         cur_func=pathtest.current_path()         return cur_func #print caller_func() #print def_func() #print real_func() #print curr_func()

打印结果:

/soft/testcase/group01

/soft/testcase/group01

/soft/src

/soft/src

3.  test01.py

cd /soft/bin

vim test01.py

import os,sys,inspect sys.path.append("../src") import aaa def func_a():         return aaa.caller_func() def func_b():         return aaa.def_func() def func_c():         return aaa.real_func() def func_d():         return aaa.curr_func() print func_a() print func_b() print func_c() print func_d()

打印结果:

/soft/testcase/group01

/soft/testcase/group01

/soft/bin

/soft/src



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有